<template>
    <a-spin :spinning="confirmLoading">
        <j-form-container :disabled="formDisabled">
            <a-form :form="form" slot="detail">
                <a-row>
                    <a-col :span="24">
                        <a-form-item label="排序号" :labelCol="labelCol" :wrapperCol="wrapperCol">
                            <a-input-number style="width:100%;" :min="1" v-decorator="[
                                'fs_fz_xh',
                                { rules: [{ required: true, message: '请填写排序号！' }] },]" 
                                placeholder="请输入排序号"  >
                            </a-input-number>
                        </a-form-item>
                    </a-col>
                    <a-col :span="24">
                         <a-form-item label="分组编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
                            <a-input v-decorator="['fs_fz_bh',{ rules: [{ required: true, message: '请填写分组编号！' }] }]" placeholder="请输入分组编号"></a-input>
                        </a-form-item>
                    </a-col>
                    <a-col :span="24">
                        <a-form-item label="分组名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
                            <a-input v-decorator="['fs_fz_mc',{ rules: [{ required: true, message: '请填写分组名称！' }] }]" placeholder="请输入分组名称"></a-input>
                        </a-form-item>
                    </a-col>
                    <a-col :span="24">
                        <a-form-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol">
                            <a-input v-decorator="['bz1',{ rules: [{ required: true, message: '请填写备注！' }] }]" placeholder="请输入备注"></a-input>
                        </a-form-item>
                    </a-col>
                    <a-col :span="24">
                        <a-form-item label="分组类别" :labelCol="labelCol" :wrapperCol="wrapperCol">
                            <a-select v-decorator="[
                                'fl_id',
                                { rules: [{ required: true, message: '请选择分组类别！' }] },
                            ]"
                            placeholder="请选择分组类别">
                                <a-select-option :value="item.fl_id" v-for="(item, index) in cglylist" :key="index">
                                    {{item.fl_mc}}
                                </a-select-option>
                            </a-select>
                        </a-form-item>
                    </a-col>
                    <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
                        <a-button @click="submitForm">提 交</a-button>
                    </a-col>
                </a-row>
            </a-form>
        </j-form-container>
    </a-spin>
</template>

<script>

import { httpAction, getAction } from '@/api/manage'
import pick from 'lodash.pick'
import { validateDuplicateValue } from '@/utils/util'
import {
    sysCxjFlCxjsbList
} from "@/api/api";
export default {
    name: 'CxjFsFzForm',
    components: {},
    props: {
        //流程表单data
        formData: {
            type: Object,
            default: ()=>{},
            required: false
        },
        //表单模式：true流程表单 false普通表单
        formBpm: {
            type: Boolean,
            default: false,
            required: false
        },
        //表单禁用
        disabled: {
            type: Boolean,
            default: false,
            required: false
        }
    },
    data () {
        return {
            cglylist: [],
            form: this.$form.createForm(this),
            model: {},
            labelCol: {
                xs: { span: 24 },
                sm: { span: 5 },
            },
            wrapperCol: {
                xs: { span: 24 },
                sm: { span: 16 },
            },
            confirmLoading: false,
            validatorRules: {},
            url: {
                add: "/cxj/cxjFsFz/add",
                edit: "/cxj/cxjFsFz/edit",
                queryById: "/cxj/cxjFsFz/queryById"
            }
        }
    },
    computed: {
        formDisabled(){
            if(this.formBpm===true){
                if(this.formData.disabled===false){
                    return false
                }
                return true
            }
            return this.disabled
        },
        showFlowSubmitButton(){
            if(this.formBpm===true){
                if(this.formData.disabled===false){
                    return true
                }
            }
            return false
        }
    },
    created () {
        //如果是流程中表单，则需要加载流程表单data
        this.showFlowData();
        this.getCxjsbCglylist();
    },
    methods: {
         // 获取成果来源
        getCxjsbCglylist(){
            sysCxjFlCxjsbList().then(res=>{
                let data = res.data;
                if (res.success) {
                    this.cglylist = res.result;
                }
            })
        },
        add () {
            this.edit({});
        },
        edit (record) {
            this.form.resetFields();
            this.model = Object.assign({}, record);
            this.visible = true;
            console.log('wqe2qweq',this.model)
            this.$nextTick(() => {
                this.form.setFieldsValue(pick(this.model,'fl_id','fs_fz_xh','fs_fz_bh','fs_fz_mc','cxj_fs_hc_id','shijian','is_valid','fs_fz_hcmc','fs_fz_hcdd','bz1','bz2'))
            })
        },
        //渲染流程表单数据
        showFlowData(){
            if(this.formBpm === true){
                let params = {id:this.formData.dataId};
                getAction(this.url.queryById,params).then((res)=>{
                    if(res.success){
                        this.edit (res.result);
                    }
                });
            }
        },
        submitForm () {
            const that = this;
            // 触发表单验证
            this.form.validateFields((err, values) => {
                if (!err) {
                    that.confirmLoading = true;
                    let httpurl = '';
                    let method = '';
                    if(!this.model.id){
                        httpurl += this.url.add;
                        method = 'post';
                    }else{
                        httpurl+=this.url.edit;
                        method = 'put';
                        // formData.id = this.model.id;
                    }
                    let formData = Object.assign(this.model, values);
                    console.log("表单提交数据",formData)
                    httpAction(httpurl,formData,method).then((res)=>{
                        if(res.success){
                            that.$message.success(res.message);
                            that.$emit('ok');
                        }else{
                            that.$message.warning(res.message);
                        }
                    }).finally(() => {
                        that.confirmLoading = false;
                    })
                }
            })
        },
        popupCallback(row){
            this.form.setFieldsValue(pick(row,'fl_id','fs_fz_xh','fs_fz_bh','fs_fz_mc','cxj_fs_hc_id','shijian','is_valid','fs_fz_hcmc','fs_fz_hcdd','bz1','bz2'))
        },
    }
}
</script>